//
//  FunctionIntroManager.m
//  Coding_iOS
//
//  Created by Ease on 15/8/6.
//  Copyright (c) 2015年 Coding. All rights reserved.
//

#define kIntroPageKey @"intro_page_version"
#define kIntroPageNum 3
#define kSkipButtonY kDevice_Is_iPhone6Plus ? 115 : kDevice_Is_iPhone6 ?  107 : 92

#import "FunctionIntroManager.h"
#import "EAIntroView.h"
#import <Masonry/Masonry.h>

@interface FunctionIntroManager ()<EAIntroDelegate>

@property (strong, nonatomic) EAIntroView *introView;

@end

@implementation FunctionIntroManager

+ (void)showIntroPage
{
    if ([self needToShowIntro]) {
        FunctionIntroManager *manager = [FunctionIntroManager shareManager];
        [manager.introView showFullscreen];
        [self markHasBeenShowed];
    }
}

+ (BOOL)needToShowIntro
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *preVersion = [defaults stringForKey:kIntroPageKey];
    BOOL needToShow = ![preVersion isEqualToString:kVersionBuild_Coding];
    return needToShow;
}

+ (void)markHasBeenShowed
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:kVersionBuild_Coding forKey:kIntroPageKey];
    [defaults synchronize];
}

#pragma mark private M

+ (instancetype)shareManager
{
    static FunctionIntroManager *shared_manager = nil;
    static dispatch_once_t pred;
    dispatch_once(&pred, ^{
        shared_manager = [self new];
    });
    return shared_manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        NSMutableArray *pages = [NSMutableArray new];
        for (int index = 0; index < kIntroPageNum; index ++) {
            EAIntroPage *page = [self p_pageWithIndex:index];
            [pages addObject:page];
        }
        _introView = [[EAIntroView alloc] initWithFrame:[UIScreen mainScreen].bounds andPages:pages];
        _introView.backgroundColor = [UIColor whiteColor];
        _introView.swipeToExit = NO;
        _introView.pageControl.hidden = YES;
        _introView.scrollView.bounces = YES;
        _introView.skipButtonAlignment = EAViewAlignmentCenter;
        _introView.skipButton = [self p_useImmediatelyButton];
        _introView.skipButtonY = kSkipButtonY;
        _introView.delegate = self;
       
    }
    return self;
}

- (UIButton *)p_useImmediatelyButton
{
    UIButton *button = [UIButton new];
    [button addTarget:self action:@selector(dismissIntroView) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 160, 35)];
   
    button.layer.cornerRadius = 10;
    button.clipsToBounds = YES;
    
    button.layer.borderColor = kColorMineBlueBG.CGColor;
    button.layer.borderWidth = 1;
    
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    button.backgroundColor = [UIColor whiteColor];
    [button setTitleColor:kColorMineBlueBG forState:UIControlStateNormal];
    [button setTitleColor:[UIColor lightTextColor] forState:UIControlStateHighlighted];
    [button setTitle:@"立即体验" forState:UIControlStateNormal];
    return button;
}

- (void)dismissIntroView
{
    [self.introView hideWithFadeOutDuration:0.3];
}

- (EAIntroPage *)p_pageWithIndex:(NSInteger)index
{
    NSString *imageName = [NSString stringWithFormat:@"intro_page%ld", (long)index];
    imageName = [imageName stringByAppendingString:(kDevice_Is_iPhone6Plus? @"_ip6+":kDevice_Is_iPhone6? @"_ip6": @"_ip5")];
    EAIntroPage *page = [EAIntroPage page];
    page.bgImage = [UIImage imageNamed:imageName];
    return page;
}

#pragma mark EAIntroDelegate

- (void)intro:(EAIntroView *)introView pageStartScrolling:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex
{
    
}
- (void)intro:(EAIntroView *)introView pageAppeared:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex
{
    
}

- (void)intro:(EAIntroView *)introView pageEndScrolling:(EAIntroPage *)page withIndex:(NSUInteger)pageIndex
{

}

@end
